<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${mapperPackage}.${mapperName}">
    <insert id="insert" parameterType="${entityPackage}.${entityName}">
        insert into ${tableName} (
            <#assign i1 = 0><#list columns as column><#if i1 != 0>,</#if>${column.name}<#assign i1 = i1+1></#list>
        )
        values(
            <#assign i2 = 0><#list columns as column><#if i2 != 0>,</#if><#noparse>#{</#noparse>${column.javaFiled}<#noparse>}</#noparse><#assign i2 = i2+1></#list>
        )
    </insert>

    <insert id="insertBatch">
        insert into ${tableName} (
            <#assign i1 = 0><#list columns as column><#if i1 != 0>,</#if>${column.name}<#assign i1 = i1+1></#list>
        )
        values
        <foreach collection="list" open="(" close=")" item="item">
            <#assign i2 = 0><#list columns as column><#if i2 != 0>,</#if><#noparse>#{</#noparse>item.${column.javaFiled}<#noparse>}</#noparse><#assign i2 = i2+1></#list>
        </foreach>
    </insert>

    <#if primaryColumn?? && (primaryColumn?size == 1)>
    <delete id="deleteById">
        delete from ${tableName}
        <where>
            <#list primaryColumn as column>
            and ${column.name} = <#noparse>#{</#noparse>_parameter}
            </#list>
        </where>
    </delete>

    <update id="updateById" parameterType="${entityPackage}.${entityName}">
        update ${tableName}
        set <#assign i1 = 0>
        <#list normalColumn as column>
            <#if i1 != 0>,</#if>${column.name} = <#noparse>#{</#noparse>${column.javaFiled}}<#assign i1 = i1+1>
        </#list>
        <where>
            <#list primaryColumn as column>
            and ${column.name} = <#noparse>#{</#noparse>${column.javaFiled}}
            </#list>
        </where>
    </update>

    <update id="updateSelectiveById" parameterType="${entityPackage}.${entityName}">
         update ${tableName}
         <set>
             <#list normalColumn as column>
             <if test="${column.javaFiled} != null">
                 ${column.name} = <#noparse>#{</#noparse>${column.javaFiled}},
             </if>
             </#list>
         </set>
        <where>
            <#list primaryColumn as column>
            and ${column.name} = <#noparse>#{</#noparse>${column.javaFiled}}
            </#list>
        </where>
    </update>

    <select id="selectById" resultType="${entityPackage}.${entityName}">
         select <#assign i1 = 0> <#list columns as column><#if i1 != 0>,</#if>${column.name}<#assign i1 = i1+1></#list>
         from ${tableName}
        <where>
            <#list primaryColumn as column>
            and ${column.name} = <#noparse>#{</#noparse>_parameter}
            </#list>
        </where>
    </select>
    </#if>

</mapper>
